package kuaidi.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kuaidi.api.services.KeFuManager;
import kuaidi.entity.kuaidijieguo.kefu.KeFu;
import kuaidi.helper.R;
import kuaidi.view.activity.KeFuActivity;
import kuaidi.view.adapter.KeFuAdapter;

/**
 * A simple {@link Fragment} subclass.
 */
public class KefuFragment extends Fragment {


    @BindView(R.id.kefu_xrcv)
    RecyclerView kefuXrcv;

    public KefuFragment() {
        // Required empty public constructor
    }

    private List<KeFu> data;
    private KeFuAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_kefu, container, false);
        ButterKnife.bind(this, view);
        initData();
        createData();
        return view;
    }

    private void createData() {
        List<KeFu> keFuInfo = KeFuManager.getiKuaidiModel().getKeFuInfo();
        data.addAll(keFuInfo);
        adapter.notifyDataSetChanged();
        kefuXrcv.setLayoutManager(new GridLayoutManager(getContext(), 1));
        kefuXrcv.setAdapter(adapter);
        adapter.setOnItemClickListener(new KeFuAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, KeFu data) {
                Intent intent = new Intent(getActivity(), KeFuActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("info", data);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        data = new ArrayList<>();
        adapter = new KeFuAdapter(data, getContext());
    }

}
